package com.yc.util;

/**
 * @program: lite_monitoring
 * @description: 命令行参数解析器 ，格式要求:  java -jar xxx.jar --arg1=value1 --arg2=value2
 * @author: zy
 * @create: 2025-03-19 21:34
 */
public class CommandLineArgsParser {
    public static CommandLineArgs parse(String... args) {
        CommandLineArgs commandLineArgs = new CommandLineArgs();
        for (String arg : args) {
            if (arg.startsWith("--")) {
                String argText = arg.substring(2, arg.length());
                String argName;
                String argValue = null;
                if (argText.contains("=")) {
                    argName = argText.substring(0, argText.indexOf("="));
                    argValue = argText.substring(argText.indexOf("=") + 1,
                            argText.length());
                } else {
                    argName = argText;
                }
                if (argName.isEmpty()
                        || (argValue != null && argValue.isEmpty())) {
                    throw new IllegalArgumentException("Invalid argument syntax: " + arg);
                }
                commandLineArgs.addArg(argName, argValue);
            }

        }
        return commandLineArgs;
    }

}
